package com.softtown.modules.sys.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.exception.RenException;
import com.softtown.common.utils.Result;
import com.softtown.modules.oss.cloud.AbstractCloudStorageService;
import com.softtown.modules.oss.cloud.OSSFactory;
import com.softtown.modules.sys.service.SysResService;
import com.softtown.modules.sys.vo.SysResVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: SysResController
 * @Description: 系统动态资源Controller层
 * @Author: fuming
 * @Date: 2019/6/22 21:15
 */
@RestController
@RequestMapping("/sys/res")
@Api(tags = "动态资源")
public class SysResController {

    @Resource
    private SysResService sysResService;

    /**
     * @Title: getPicResUrls
     * @Description: 获取走马灯图片
     * @param @param []
     * @return @return com.softtown.common.utils.Result<com.softtown.modules.sys.vo.SysResVO>
     * @author fuming
     * @throws
     * @date 2019/6/23 10:01
     */
    @PostMapping("getPics")
    @ApiOperation("获取平台走马灯图片")
    @LogOperation("获取平台走马灯图片")
    public Result<SysResVO> getPicResUrls() {
        //其实获取某个动态资源应该靠use_id来查询,但现在没有自有平台，所以先直接获取
        SysResVO notablePicsResUrl = sysResService.getNotablePicsResUrl();
        if(notablePicsResUrl !=null && notablePicsResUrl.getResUrl() !=null){
            if (!"".equals(notablePicsResUrl.getResUrl())){
                String[] resUrl = notablePicsResUrl.getResUrl().split(",");
                List<String> urlList = new ArrayList<>();

                AbstractCloudStorageService service = OSSFactory.build();
                if(service == null){
                    throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR);
                }
                for (String url :resUrl){
                    urlList.add(service.getHeadUrl(url));
                }
                String[] newResUrl =urlList.toArray(new String[urlList.size()]);
                List<String> result = Arrays.asList(newResUrl);
                notablePicsResUrl.setNotablePicsResUrlList(result);
            }
        }
        return new Result<SysResVO>().ok(notablePicsResUrl);
    }
}
